<template>
  <section>

    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters" ref="formRef">
        <el-form-item label="关键字">
          <el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>



        <span class="demonstration">&#12288 创建时间</span>
        <el-date-picker
            v-model="filters.createtime"
            type="datetime"
            value-format="yyyy-MM-dd"
            placeholder="创建时间"
            format="yyyy-MM-dd"
        >
        </el-date-picker>
        <el-form-item>
          <el-button type="primary" @click="getTableData">查询</el-button>
          <el-button type="danger" @click="resetForm('formRef')">重置</el-button>
          <el-button type="primary" @click="addData">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="tableData"
              highlight-current-row
              @selection-change="selsChange"
              border
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60" label="序号">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="190">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="190">
      </el-table-column>
      <el-table-column prop="salt" label="盐值" width="190">
      </el-table-column>
      <el-table-column prop="password" label="密码" width="190">
      </el-table-column>

      <el-table-column prop="age" label="年龄" width="190">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="170" sortable>
      </el-table-column>



      <el-table-column prop="headImg" label="头像" width="190">
      </el-table-column>
      <el-table-column prop="state" label="状态" width="150">
        <!-- 如果需要对表中某个字段进行显示内容的切换，则可以使用<template>语法进行转换 -->
        <!-- 分析：如果是0正常；如果是1：禁用-->
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.state == 0" type="success">正常</el-tag>
          <el-tag size="mini" v-if="scope.row.state == 1" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--弹框界面-->
    <el-dialog :title="titleValue" :visible.sync="dialogTableVisible">
      <el-form :model="addForm" label-width="80px" :rules="formRules" ref="addForm">
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="addForm.username" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="name">
          <el-input v-model="addForm.email" auto-complete="off"></el-input>
        </el-form-item>




        <el-form-item label="手机号" prop="name">
          <el-input v-model="addForm.phone" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="盐值" prop="name">
          <el-input v-model="addForm.salt" auto-complete="off"></el-input>
        </el-form-item>



        <el-form-item label="密码" prop="name">
          <el-input v-model="addForm.password" auto-complete="off"></el-input>
        </el-form-item>



        <el-form-item label="状态">
          <el-radio-group v-model="addForm.state">
            <el-radio class="radio" label="1">禁用</el-radio>
            <el-radio class="radio" label="0">正常</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="年龄" prop="name">
          <el-input v-model="addForm.age" auto-complete="off"></el-input>
        </el-form-item>

        <div class="block">
          <span class="demonstration">&#12288 创建时间</span>
          <el-date-picker
              v-model="addForm.createTime"
              type="datetime"
              placeholder="创建时间"
              format="yyyy-MM-dd">
          </el-date-picker>
        </div>

        <!--文件上传-->
        <el-form-item prop="headImg" label="文章封面">
          <el-upload
              class="upload-demo"
              action="http://localhost:80/file/upload"
              name="fileName"
              :on-remove="handleRemove"
              :on-success="handleSuccess"
              :on-exceed="exceed"
              :file-list="fileList"
              :limit=1
              accept=".jpg,.png"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过2MB</div>
          </el-upload>
        </el-form-item>

      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button @click.native="dialogTableVisible = false">取消</el-button>
        <el-button type="primary" @click.native="onSubmit">提交</el-button>
      </div>
    </el-dialog>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--
        @size-change：选择了每页条数 ，触发handleSizeChange方法
        @current-change：选择了第几页 ，触发handleCurrentChange方法
        :@current-change：把当前页绑定到  current 变量
        :page-size="size" ：每页显示的条数
        :layout  ：分页条布局
        :total="tableTotal" ：总条数
     -->
      <el-pagination
          style="float: right;margin: 20px"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="filters.current"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="filters.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </el-col>
  </section>
</template>

<script>
  import {batchRemoveUser} from "../../api/api";

  export default {
    name: 'Department',
    data() {
      return {
        fileList: [],
        titleValue: '',
        filters:{
          keyword: '',
          current: 1, //默认第几页
          size: 10, //默认每页显示条数
          createTime:''
        },
        tableData: [],
        optionEmpData:[],
        optionuserData: [], //下拉部门选项集合
        total: 0,//列表总数量
        dialogTableVisible: false, //表单弹框是否展示
        sels: [],//列表选中列数据
        formRules: { //表单校验规则
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ]
        },
        addForm:{
            username:'',
          email:'',
          phone:'',
          salt:'',
          password:'',
          state:'',
          age:'',
          createTime:'',
          headImg:'',
        },//表单对象
      }
    },
    //定义所有的方法
    methods: {
      //文件上传成功之后，会调用此方法
      handleSuccess(response, file, fileList){
        console.log("文件上传成功");
        console.log(response);
        if(response.success){
          console.log(response.data.data.fileFullPath);
          console.log(response.data);
          console.log(response.data);
          console.log("1111111111");
          this.addForm.articlePic = response.data.data.fileFullPath;
        }else{
          this.$message({
            message: response.message,
            type: 'error'
          });
        }
      },
      handleRemove(file, fileList) {
        console.log("-1-")
        console.log(file);
        this.addForm.articlePic='';
        this.$http.post("/file/deleteFile",file).then(result => {
        });
        console.log("删除文件");
        console.log(file);//可以取到上传文件的名称和OSS存储全路径
      },
      handlePreview(file) {
        console.log(file);
      },
      //点击复选框时触发
      selsChange(sels) {
        this.sels = sels;
      },
      //重置表单
      resetForm() {
        this.filters = {};
      },
      //点击编辑按钮触发
      handleEdit(index, row){
        this.titleValue = '编辑用户';
        //显示弹框
        this.dialogTableVisible = true;
        console.log(row);
        this.addForm = Object.assign({}, row);//属性拷贝
      },
      //提交表单
      onSubmit(){
        if(this.addForm.id){
          //id有值，做修改
          //发请求
          this.$http.post("/user/saveOrUpdate", this.addForm).then(res=>{
            //隐藏弹框
            this.dialogTableVisible = false;
            //刷新列表
            this.getTableData();
            //提示信息
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }else{
          //id没有值，做新增操作
          //发请求
          this.$http.post("/user/saveOrUpdate", this.addForm).then(res=>{
            //隐藏弹框
            this.dialogTableVisible = false;
            //刷新列表
            this.getTableData();
            //提示信息
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }
      },
      //点击新增
      addData(){
        //显示弹框
        this.addForm = {};
        this.dialogTableVisible = true;
        this.titleValue = '新增部门';
        //清空表单数据

      },
      //删除
      handleDelete(index, row){
        this.$confirm('确定删除此记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //点击确定执行下面代码
          //发请求
          this.$http.delete("/user/delete/" + row.id, null).then(res=>{
            this.getTableData();//刷新列表数据
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }).catch(error => {
          console.log(error);
        });
      },
      //批量删除
      batchRemove() {
        //从sels中获取id值形成集合，传给后台做批量删除
        var ids = this.sels.map(item => item.id);
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          let param = { ids: ids };
          //发请求
          this.$http.post("/user/deleteBatch", param).then(res=>{
            this.getTableData();//刷新列表数据
            if(res.data.success){
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'success'
              });
            }else{
              this.$notify({
                title: '提示',
                message: res.data.msg,
                type: 'error'
              });
            }
          }).catch(error => {
            console.log(error);
          });
        }).catch(error => {
          console.log(error);
        });
      },
      //获取列表数据
      getTableData(){
        //发请求
        this.$http.post("/user/pagelist", this.filters).then(res=>{
          if(res.data.success == true){
            this.tableData = res.data.data.list;
            this.total = res.data.data.total;
          }
        }).catch(error => {
          console.log(error);
        });
      },
      //选择每页条数时触发
      handleSizeChange(val){
        console.log('选择了每页条数=' + val);
        this.filters.size = val;
        this.getTableData();
      },
      //翻页触发该函数
      handleCurrentChange(val){
        console.log('翻页了=' + val);
        this.filters.current = val;
        this.getTableData();
      },
      findAllEmployee(){
        this.$http.get('/emp/list').then(res=>{
          this.optionEmpData = res.data.data;
        }).catch(error => {
          console.log(error);
        });
      }
    },
    //钩子函数：数据初始化
    mounted() {
      this.getTableData();
      //查询所有部门信息
      this.findAlluser();
      this.findAllEmployee();
    }
  }
</script>

<style scoped>
</style>